home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / CropImageFilter.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  141 lines

  1. /*
  2.  * @(#)CropImageFilter.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17. import java.awt.image.ImageConsumer;
  18. import java.awt.image.ColorModel;
  19. import java.util.Hashtable;
  20. import java.awt.Rectangle;
  21.  
  22. /**
  23.  * An ImageFilter class for cropping images.
  24.  * This class extends the basic ImageFilter Class to extract a given
  25.  * rectangular region of an existing Image and provide a source for a
  26.  * new image containing just the extracted region.  It is meant to
  27.  * be used in conjunction with a FilteredImageSource object to produce
  28.  * cropped versions of existing images.
  29.  *
  30.  * @see FilteredImageSource
  31.  * @see ImageFilter
  32.  *
  33.  * @version    1.6 07/01/98
  34.  * @author     Jim Graham
  35.  */
  36. public class CropImageFilter extends ImageFilter {
  37.     int cropX;
  38.     int cropY;
  39.     int cropW;
  40.     int cropH;
  41.     
  42.     /**
  43.      * Constructs a CropImageFilter that extracts the absolute rectangular
  44.      * region of pixels from its source Image as specified by the x, y,
  45.      * w, and h parameters.
  46.      * @param x the x location of the top of the rectangle to be extracted
  47.      * @param y the y location of the top of the rectangle to be extracted
  48.      * @param w the width of the rectangle to be extracted
  49.      * @param h the height of the rectangle to be extracted
  50.      */
  51.     public CropImageFilter(int x, int y, int w, int h) {
  52.     cropX = x;
  53.     cropY = y;
  54.     cropW = w;
  55.     cropH = h;
  56.     }
  57.  
  58.     /**
  59.      * Passes along  the properties from the source object after adding a
  60.      * property indicating the cropped region.
  61.      */
  62.     public void setProperties(Hashtable props) {
  63.     props = (Hashtable) props.clone();
  64.     props.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
  65.     super.setProperties(props);
  66.     }
  67.  
  68.     /**
  69.      * Override the source image's dimensions and pass the dimensions
  70.      * of the rectangular cropped region to the ImageConsumer.
  71.      * @see ImageConsumer
  72.      */
  73.     public void setDimensions(int w, int h) {
  74.     consumer.setDimensions(cropW, cropH);
  75.     }
  76.    
  77.     /**
  78.      * Determine whether the delivered byte pixels intersect the region to
  79.      * be extracted and passes through only that subset of pixels that
  80.      * appear in the output region.
  81.      */
  82.     public void setPixels(int x, int y, int w, int h,
  83.               ColorModel model, byte pixels[], int off,
  84.               int scansize) {
  85.     int x1 = x;
  86.     if (x1 < cropX) {
  87.         x1 = cropX;
  88.     }
  89.     int x2 = x + w;
  90.     if (x2 > cropX + cropW) {
  91.         x2 = cropX + cropW;
  92.     }
  93.     int y1 = y;
  94.     if (y1 < cropY) {
  95.         y1 = cropY;
  96.     }
  97.     int y2 = y + h;
  98.     if (y2 > cropY + cropH) {
  99.         y2 = cropY + cropH;
  100.     }
  101.     if (x1 >= x2 || y1 >= y2) {
  102.         return;
  103.     }
  104.     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  105.                model, pixels,
  106.                off + (y1 - y) * scansize + (x1 - x), scansize);
  107.     }
  108.     
  109.     /**
  110.      * Determine if the delivered int pixels intersect the region to
  111.      * be extracted and pass through only that subset of pixels that
  112.      * appear in the output region.
  113.      */
  114.     public void setPixels(int x, int y, int w, int h,
  115.               ColorModel model, int pixels[], int off,
  116.               int scansize) {
  117.     int x1 = x;
  118.     if (x1 < cropX) {
  119.         x1 = cropX;
  120.     }
  121.     int x2 = x + w;
  122.     if (x2 > cropX + cropW) {
  123.         x2 = cropX + cropW;
  124.     }
  125.     int y1 = y;
  126.     if (y1 < cropY) {
  127.         y1 = cropY;
  128.     }
  129.     int y2 = y + h;
  130.     if (y2 > cropY + cropH) {
  131.         y2 = cropY + cropH;
  132.     }
  133.     if (x1 >= x2 || y1 >= y2) {
  134.         return;
  135.     }
  136.     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  137.                model, pixels,
  138.                off + (y1 - y) * scansize + (x1 - x), scansize);
  139.     }
  140. }
  141.